﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _8.SequenceOfMaxSum
{
    class SequenceOfMaxSum
    {
        static void Main(string[] args)
        {
            checked
            {
                Random rand = new Random();
                Console.WriteLine("Input what length of arrays you preffer to generate random sequence:");
                int n = int.Parse(Console.ReadLine());
                int[] arrayA;
                arrayA = new int[n];
                int randomint = rand.Next(-99, 99);
                for (int i = 0; i < arrayA.Length; i++)
                {

                    arrayA[i] = randomint;
                    Console.WriteLine("arrayA[{0}] = {1}", i, arrayA[i]);
                    randomint = rand.Next(-99, 99);
                }

                int current = 0;
                StringBuilder maxSumInString = new StringBuilder();
                int maxSum = Int16.MinValue;
                string maximalSequence = "";
                for (int i = 0; i < arrayA.Length; i++)
                {
                    current += arrayA[i];
                    maxSumInString.AppendFormat("{0}, ", arrayA[i]);

                    if (current > maxSum)
                    {
                        maxSum = current;
                        maximalSequence = maxSumInString.ToString();
                    }

                    if (current < 0)
                    {
                        current = 0;
                        maxSumInString.Clear();
                    }
                }
                maximalSequence = "{" + maximalSequence + "\b\b }";
                Console.WriteLine("Maximal sum  = {0}, sequence is : {1}  ", maxSum, maximalSequence);
            }
        }
    }
}